package com.zs.service.impl;

import com.zs.dao.StudentDao;
import com.zs.dto.StudentDto;
import com.zs.entity.StudentInfo;
import com.zs.exception.UnCatchParamException;
import com.zs.service.StudentService;
import com.zs.util.IsEmptyUtils;
import com.zs.util.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.List;
import java.util.UUID;

import static com.zs.util.Common.MESSAGE_FAIL;
import static com.zs.util.Common.MESSAGE_SUCCESS;

/**
 * <p>
 *  学生impl
 * </p>
 * @Author: Shun
 * @CreateTime: 2019-07-17 19:54
 * @Emil: 381889220@qq.com
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class StudentServiceImpl implements StudentService {
    //springBoot事物 只要报错就事物回滚
    @Resource
    private StudentDao studentDao;

    //@ExceptionHandler(value = TestException.class)
    @Override
    public List<StudentInfo> search(StudentDto studentDto) {
        return studentDao.get(studentDto);
    }

    @Override
    public int getCount(StudentDto studentDto) {
        return studentDao.getCount(studentDto);
    }

    @Override
    public Result insert(StudentInfo studentInfo) {
        studentInfo.setSid(UUID.randomUUID().toString());
        Integer insertResult = studentDao.add(studentInfo);
        if (insertResult > 0) {
            return new Result(true, MESSAGE_SUCCESS);
        }

        return new Result(MESSAGE_FAIL);
    }

    @Override
    public Result delete(String[] ids) {

        Integer insertResult = studentDao.remove(ids);

        if (insertResult > 0) {
            return new Result(true, MESSAGE_SUCCESS);
        }

        return new Result(MESSAGE_FAIL);
    }

    @Override
    /* UnCatchParamException 缺少参数异常  */
    public Result update(StudentInfo studentInfo) throws UnCatchParamException {

        if (IsEmptyUtils.isEmpty(studentInfo.getSid())) {
            throw new UnCatchParamException();
        }

        Integer insertResult = studentDao.modify(studentInfo);

        if (insertResult > 0) {
            return new Result(true, MESSAGE_SUCCESS);
        }

        return new Result(MESSAGE_FAIL);
    }
}
